function [ rate ] = calcLearningRate( error, lastError, learned, total )
%CALCLEARNINGRATE Summary of this function goes here
%   Detailed explanation goes here
global improvementType;
global IMPROVEMENT_LEARNING_RATE;
global IMPROVEMENT_MOMENTUM;
global totalErrorDecrease;
global totalErrorIncrease;
global LEARNING_RATE;
global MOMENTUM;
global K_MOMEMNTUM;

delta = 0;

if(improvementType(IMPROVEMENT_LEARNING_RATE)==1)
    if(error < lastError)
        totalErrorDecrease = totalErrorDecrease+1;
        totalErrorIncrease = 0;
        if(improvementType(IMPROVEMENT_MOMENTUM) == 0)
            MOMENTUM = 0.0;
        else
             MOMENTUM = K_MOMEMNTUM;;
        end
        if(totalErrorDecrease >= 2)
            delta = 0.05;
            totalErrorDecrease = 0;
        end
    else
        totalErrorDecrease = 0;
        totalErrorIncrease = totalErrorIncrease+1;
       % delta = -0.005*totalErrorIncrease;  
        delta = -0.005*LEARNING_RATE;
        MOMENTUM = 0.0;
    end

    LEARNING_RATE = LEARNING_RATE + delta;
    if(LEARNING_RATE <= 0)
        LEARNING_RATE = 0.1;
        totalErrorIncrease = 0;
    else
        if(LEARNING_RATE >= 1)
            LEARNING_RATE = 0.9;
        end
    end
    rate = LEARNING_RATE;
else
    rate = max(1.0 - (learned/total), 0.5);
end

%rate = max(1.0 - (learned/total), 0.4);


end

